﻿using System;
using System.Web.Mvc;
using Zeenck.DataAccess;
using Zeenck.Extensibility.Attributes;
using Zeenck.Models;

namespace Zeenck.Controllers
{
    public class ResetPasswordController : Controller
    {
        #region Verify Email

        [HttpGet]
        public ActionResult VerifyEmailBegin()
        {
            return View("VerifyEmail", new VerifyEmail());
        }

        [HttpPost]
        public ActionResult VerifyEmailSubmit(VerifyEmail model)
        {
            if (ModelState.IsValid)
            {
                AccountModule.ResetPassword(model.Email, Url);

                String title = Configuration.Zeenck.Info.ResetPassword.VerifyEmailCompleteTitle;
                String message = Configuration.Zeenck.Info.ResetPassword.VerifyEmailCompleteMessage;
                var applicationMessage = new ApplicationMessage(title, message);

                return View("General/ApplicationMessage", applicationMessage);
            }
            return View("VerifyEmail", model);
        }

        [CaptchaValidator]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateComment(Int32 id, bool captchaValid)
        {
            if (!captchaValid)
            {
                ModelState.AddModelError("_FORM", "You did not type the verification word correctly. Please try again.");
                return View("VerifyEmail");
            }
            // If we got this far, something failed, redisplay form
            return View("VerifyEmail");
        }

        #endregion Verify Email

        #region Reset Password

        [HttpGet]
        public ActionResult ResetPasswordBegin(String confirmationCode)
        {
            if (AccountModule.IsValidConfirmationCode(confirmationCode))
            {
                var model = new ResetPassword { ConfirmationCode = confirmationCode };
                return View("ResetPassword", model);
            }

            String title = Configuration.Zeenck.Info.ResetPassword.ResetPasswordFailureHeader;
            String message = Configuration.Zeenck.Info.ResetPassword.ResetPasswordFailureMessage2;
            var applicationMessage = new ApplicationMessage(title, message);
            return View("General/ApplicationMessage", applicationMessage);
        }

        [HttpPost]
        public ActionResult ResetPasswordSubmit(ResetPassword model)
        {
            if (ModelState.IsValid)
            {
                String title;
                String message;

                /*if (ProfileModule.ResetPassword(model.ConfirmationCode, model.NewPassword))
                {
                    title = Zeenck.Configuration.Zeenck.Info.ResetPassword.ResetPasswordSuccessHeader;
                    message = Zeenck.Configuration.Zeenck.Info.ResetPassword.ResetPasswordSuccessMessage;
                }
                else*/
                {
                    title = Configuration.Zeenck.Info.ResetPassword.ResetPasswordFailureHeader;
                    message = Configuration.Zeenck.Info.ResetPassword.ResetPasswordFailureMessage1;
                }

                var applicationMessage = new ApplicationMessage(title, message);

                return View("General/ApplicationMessage", applicationMessage);
            }
            return View("ResetPassword", model);
        }

        #endregion Reset Password
    }
}
